<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar
      title="全部分类"
    />
    <!-- 搜索框 -->
    <van-search v-model="searchValue" placeholder="请输入搜索关键词" />
    <!-- 主体 -->
    <div class="main">
      <div class="left">
        <ul>
          <li v-for="item in goodsName" :key="item.category_id" @click="changeGoodsName(item.name)">{{ item.name }}</li>
        </ul>
      </div>
      <div class="right">
        <van-grid>
          <van-grid-item :icon="item.image.external_url" :text="item.name" v-for="item in goodsList" :key="item.image_id"/>
        </van-grid>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategoryGoods } from '@/api/goodsSearch.js'
export default {
  name: 'CategoryIndex',
  data () {
    return {
      searchValue: '',
      goodsName: [], // 从后台请求的原始数据
      goodsList: [], // 当前展示的数据
      activeName: '' // 当前被选中的数据
    }
  },
  async created () {
    const { data: { list } } = await getCategoryGoods()
    this.goodsName = list
    this.activeName = list[0].name
    console.log(this.goodsName)
    console.log(this.activeName)
  },
  methods: {
    changeGoodsName (name) {
      const { children } = this.goodsName.find(item => item.name === name)
      this.goodsList = children
      console.log(children)
    }
  }
}
</script>

<style scoped>
.main {
  display:flex;
}
.main .left {
  flex:2;
  border:1px solid pink;
}
.main .left ul {
  height:600px;
  overflow:scroll;
}
.main .left ul li {
  padding:10px 15px;
}
/* .main .left ul li:first-child {
  background:pink;
} */
.main .right {
  flex:5;
}
.active {
  background:pink;
}
</style>
